//
//  GZStatusOriginalView.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/26.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
let CZStatusCellStatusFontSize: CGFloat = 12
class GZStatusOriginalView: UIView {
    //MARK: -属性
    var  originalViewBottomConstraint: Constraint?
    var status: GZStatus?{
        didSet{
            
            iconView.sd_setImageWithURL(status?.user?.profileImageUrl, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            timeLabel.text = status?.created_at
            soucrceLabel.text = status?.source
            
            memberView.image = status?.user?.mbrankImage
            
            verifiedView.image = status?.user?.verified_typeImage
            
            //微博内容
            contentLabel.text = status?.text
            
            
            //卸卸载底部约束,根据status模型来重新约束
            originalViewBottomConstraint?.uninstall()
            if let pictureURLs = status?.storePictureURLs{
                //有原创微博图片显示
                picutreView.hidden = false
                picutreView.pictureURLs = pictureURLs
                self.snp_makeConstraints { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(picutreView).offset(CZStatusCellMargin).constraint
                }
            }else{
                picutreView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(CZStatusCellMargin).constraint
                }
            }
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        prepareUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(soucrceLabel)
        addSubview(verifiedView)
        addSubview(memberView)
        addSubview(contentLabel)
        addSubview(picutreView)
        
        //添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CZStatusCellMargin)
            make.width.height.equalTo(35)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.top.equalTo(iconView)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        soucrceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(CZStatusCellMargin)
        }
        //配图视图
        picutreView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        //约束self的高度 来控制子控件的高度
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottomConstraint = make.bottom.equalTo(picutreView).offset(CZStatusCellMargin).constraint
        }
    }
    
    //MARK: -懒加载控件
    //头像
    private lazy var iconView = UIImageView()
    /// 用户名
    private lazy var nameLabel: UILabel = UILabel( textcolor: UIColor.darkGrayColor(), font: 14)
    /// 时间
    private lazy var timeLabel: UILabel = UILabel(textcolor: UIColor.orangeColor(), font: 9)
    /// 来源
    private lazy var soucrceLabel: UILabel = UILabel(textcolor: UIColor.lightGrayColor(), font: 9)
    /// 认证图标
    private lazy var verifiedView = UIImageView()
    /// 会员等级
    private lazy var memberView = UIImageView()
    /// 微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textcolor: UIColor.darkGrayColor(), font: CZStatusCellStatusFontSize)
        label.font = UIFont.systemFontOfSize(CZStatusCellStatusFontSize)
        
        label.numberOfLines = 0
        return label
        }()
    
    //配图，不管cell是否要显示图片，先加载进来，如果模型里面不需要显示图片，就把这个配图
    //视图隐藏，要配图，就显示出来
    private lazy var picutreView: GZStatusPictureView = GZStatusPictureView()
    
}
